package com.example.demofin.serveice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demofin.controller.UsersController;
import com.example.demofin.mapper.ProductMapper;
import com.example.demofin.pojo.Product;
import com.example.demofin.serveice.IProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ProductService implements IProductService {

    @Resource
    private ProductMapper productMapper;
    private final static Logger logger = LoggerFactory.getLogger(ProductService.class);
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    @Override
    public Product selectProductByCph(String chapbhao) {
        if (chapbhao == null || "".equals(chapbhao)){
            throw new IllegalStateException("ERROR：产品编号不能为空！");
        }

        Product product = null;
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        try{
            queryWrapper.eq("CHAPBHAO", chapbhao);
            product = productMapper.selectOne(queryWrapper);
        }catch (Exception e){
            throw new IllegalStateException("ERROR：查询失败，请检查输入！");
        }
        return product;
    }

    @Override
    public IPage<Product> list(Integer pageNum, Integer pageSize, Product product) throws IllegalAccessException {
        Page<Product> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();

        IPage<Product> result = null;
        Field[] fields = Product.class.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object fieldValue = field.get(product);
            if (fieldValue != null){
                if("JILUZTAI".equals(fieldName) && product.getJiluztai().equals("0")){
                    wrapper.eq("JILUZTAI", 0);
                    String nowDate = sdf.format(new Date());
                    wrapper.le("CAPCLIRQ", nowDate);
                }else {
                    wrapper.like(fieldName, fieldValue);
                }
            }
        }

        return productMapper.selectPage(page, wrapper);
    }

    @Override
    public String save(Product product) {
        int count = productMapper.insert(product);
        if (count <= 0){
            logger.info("保存失败: {}", product.toString());
            return "新增失败，请检查产品信息！！！";
        }
        return "保存成功";
    }

    @Override
    public String update(Product product) {
        int count = productMapper.updateById(product);
        if (count <= 0){
            logger.info("修改失败: {}", product.toString());
            return "修改失败,请检查修改编号！！！";
        }
        return "修改成功";
    }


}
